package com.zh.test.IO;

import net.sf.json.JSONObject;

import java.io.IOException;
import java.net.Socket;
import java.util.Date;
import java.util.HashMap;

public class IOClient {

    public static void main(String[] args) {
        Socket socket = null;
        try {
            socket = new Socket("127.0.0.1", 8000);
        } catch (IOException e) {
            e.printStackTrace();
        }
        HashMap h = new HashMap();
        h.put("topic","干脆将请求常态化的了");
        h.put("content","模拟从浏览器到服务器端的整个过程");
        JSONObject jsonObject = JSONObject.fromObject(h);

        try {
            socket.getOutputStream().  write(jsonObject.toString().getBytes());
            socket.getOutputStream().flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

        /*new Thread(() -> {
            try {
                Socket socket = new Socket("127.0.0.1", 8000);
                while (true) {
                    try {
                        HashMap h = new HashMap();
                        h.put("topic","这是标题");
                        h.put("content","这是内容");
                        JSONObject jsonObject = JSONObject.fromObject(h);
                        socket.getOutputStream().write(jsonObject.toString().getBytes());
                        socket.getOutputStream().flush();
                        Thread.sleep(2000);
                        System.out.println("发送请求");
                    } catch (Exception e) {
                    }
                }
            } catch (IOException e) {
            }
        }).start();*/
    }
}